package com.xiaoyu.izf.controller.api;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import com.xiaoyu.izf.component.AliyunSmsComponent;
import com.xiaoyu.izf.config.SysConfig;
import com.xiaoyu.izf.model.AppSendMsgCode;
import com.xiaoyu.izf.model.page.FormResult;
import com.xiaoyu.izf.service.AppSendMsgCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/send_msg")
public class SendMsgApiController {
    @Autowired
    private AliyunSmsComponent aliyunSmsComponent;
    @Autowired
    private AppSendMsgCodeService appSendMsgCodeService;

    @RequestMapping("/app")
    @ResponseBody
    public FormResult<String> appSend(HttpServletRequest request, String phone, String checkCode){
        FormResult<String> fr = new FormResult<>();
        String cid = request.getHeader("cid");
        if(StringUtils.isEmpty(cid)){
            fr.setStatus(500);
            fr.setMessage("非法访问。");
            return fr;
        }
        AppSendMsgCode appCode = appSendMsgCodeService.getOne(cid);
        if(StringUtils.isEmpty(checkCode)){
            fr.setStatus(500);
            fr.setMessage("验证码不能为空。");
            return fr;
        }
        if(StringUtils.isEmpty(appCode.getCheckCode())||(System.currentTimeMillis()-appCode.getInsertTime().getTime()>20*60*1000)){
            fr.setStatus(500);
            fr.setMessage("验证码已过期。");
            return fr;
        }
        if(!appCode.getCheckCode().toLowerCase().equals(checkCode.toLowerCase())){
            fr.setStatus(500);
            fr.setMessage("验证码错误。");
            return fr;
        }
        if(!Validator.isMobile(phone)){
            fr.setStatus(500);
            fr.setMessage("手机号码不正确。");
            return fr;
        }
        String msgCode = RandomUtil.randomNumbers(6);
        appSendMsgCodeService.setMsgCode(cid,msgCode);
        aliyunSmsComponent.sendRegSms(phone,msgCode);
        fr.setStatus(200);
        fr.setMessage("ok");
        return fr;
    }

    @RequestMapping("/web")
    @ResponseBody
    public FormResult<String> sendMsgCode(HttpServletRequest request, String mobile, String checkCode) {
        FormResult<String> fr = new FormResult<String>();
        if(!Validator.isMobile(mobile)){
            fr.setStatus(500);
            fr.setMessage("手机号码不正确。");
            return fr;
        }
        if(StringUtils.isEmpty(checkCode)){
            fr.setStatus(500);
            fr.setMessage("验证码不能为空。");
            return fr;
        }
        String code = (String)request.getSession().getAttribute(SysConfig.userCheckCodeKey);
        if(StringUtils.isEmpty(code)){
            fr.setStatus(500);
            fr.setMessage("验证码已过期。");
            return fr;
        }else{
            if(!code.toLowerCase().equals(checkCode.toLowerCase())){
                fr.setStatus(500);
                fr.setMessage("验证码错误。");
                return fr;
            }
        }
        String msgCode = RandomUtil.randomNumbers(6);
        System.out.println("code:"+msgCode);
        aliyunSmsComponent.sendRegSms(mobile, msgCode);
        request.getSession().setAttribute("msgCodeSessinId", msgCode);
        fr.setStatus(200);
        fr.setMessage("ok");
        return fr;
    }

}
